home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 925 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: news.ultranet.com!usenet
  2. From: Ken Brady <kbrady@ultranet.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Are ostream.write and istream.read non-reciprocal??
  5. Date: Mon, 8 Jan 1996 08:53:02 EST
  6. Organization: UltraNet Communications, Inc.
  7. Message-ID: <60ii536@kbrady.ultranet.com>
  8. Reply-To: Ken Brady <kbrady@ultranet.com>
  9. NNTP-Posting-Host: 205.246.13.244
  10. X-Mailer: Post Road Mailer (Green Edition Ver 1.05a)
  11.  
  12. I have an array of char which I want to store on a stream.  The array  
  13. is a string, 0-terminated; however, the length of the array may be  
  14. longer than the length of the string, i.e., I may have some binary data  
  15. following the string.  I use
  16.  
  17. { ...
  18. int len = ... // whatever length I need
  19. char *p = new p[len]
  20. //  move binary data into p; may include zero values
  21. fstream f(fname,ios::trunc | ios::in | ios::out | ios::binary);
  22. f.write((char*)(&len),sizeof(len));
  23. f.write(p,len);
  24. ..}
  25.  
  26. Later, to retrieve the data, I use
  27.  
  28. {...
  29. fstream f(fname,ios::in | ios::binary);
  30. f.read((char*)(&len),sizeof(len));
  31. p = new char[len+1];
  32. f.read(p,len);
  33. ..}
  34.  
  35.  
  36. While the value of 'len' is correct after reading it from the istream,  
  37. I find that after reading the character array my stream is out of  
  38. registration, i.e., I've read too few or too many bytes, and the  
  39. subsequent data on the stream cannot be read.  The stream seems to be  
  40. doing some sort of string-formatting even though I've specified it as  
  41. binary.
  42.  
  43. Are ostream.write and istream.read non-reciprocal??  How can I write a  
  44. simple array of arbitrarily-valued char without resorting to a slow  
  45. loop of put(p[i])??
  46.  
  47. Advice much appreciated ...
  48.  
  49.  
  50.  
  51.      __________ `-'.
  52.   /| o  o O   o  ___:
  53.     \ O ____0  /
  54.      (//W   ||
  55.      |||    ||     -CyberPriestess-
  56.      ""     ""
  57.  DONT HAVE A COW, MAN
  58.  
  59.  
  60.